package com.example.administrator.orchardeveryday.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.orchardeveryday.R;
import com.example.administrator.orchardeveryday.adapter.CarListAdapter;
import com.example.administrator.orchardeveryday.bean.dbbean.Goods;
import com.example.administrator.orchardeveryday.bean.dbbean.User;
import com.example.administrator.orchardeveryday.dao.DaoFactory;
import com.example.administrator.orchardeveryday.utils.FragmentTabUtils;
import com.loopj.android.http.AsyncHttpClient;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarFragment extends Fragment implements View.OnClickListener{

    private RelativeLayout rlNotEmpty;
    private LinearLayout llEmpty;
    private ImageView deleteIv;
    private TextView goHomeTv;
    private ListView carListView;
    private TextView totalMoneyTv;
    private Button payBtn;
    private double totalMoney;
    ArrayList<Goods> goodses=new ArrayList<>();
    CarListAdapter carListAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        saveData();
        goodses=DaoFactory.getUserDao().findGoods(1);
        View view=inflater.inflate(R.layout.fragment_car, container, false);
        initView(view);
       if(goodses.size()!=0)
        {
            llEmpty.setVisibility(View.GONE);
            rlNotEmpty.setVisibility(View.VISIBLE);
            showData();
        }
        return view;
    }

    private void initView(View view) {
        llEmpty= (LinearLayout) view.findViewById(R.id.fratment_car_empty_ll);
        rlNotEmpty= (RelativeLayout) view.findViewById(R.id.fratment_car_notempty_rl);
        deleteIv= (ImageView) view.findViewById(R.id.fragment_car_title_delete_iv);
        goHomeTv= (TextView) view.findViewById(R.id.fragment_car_gohome_btn);
        carListView= (ListView) view.findViewById(R.id.fragment_car_notempty_lv);
        totalMoneyTv= (TextView) view.findViewById(R.id.fragment_car_notempty_totalmoney_tv);
        payBtn= (Button) view.findViewById(R.id.fragment_car_notempty_pay_btn);
        for (int i = 0; i <goodses.size() ; i++) {
            totalMoney+=goodses.get(i).getPrice()*goodses.get(i).getNumber();
        }
        totalMoneyTv.setText("￥"+totalMoney);
        deleteIv.setOnClickListener(this);
        goHomeTv.setOnClickListener(this);
        payBtn.setOnClickListener(this);
    }


    private void showData() {
        carListAdapter=new CarListAdapter(goodses,getActivity());
        carListView.setAdapter(carListAdapter);
    }

    private void saveData() {
        User user=new User(111,"luo","123");
        Goods goods1=new Goods(1,"云南树莓",79,1);
        Goods goods2=new Goods(1,"云南蒙自石榴",39,1);
        Goods goods3=new Goods(1,"云南蒙自石榴",39,1);
        DaoFactory.getUserDao().save(user);
        DaoFactory.getUserDao().save(goods1);
        DaoFactory.getUserDao().save(goods2);
        DaoFactory.getUserDao().save(goods3);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.fragment_car_title_delete_iv:
                DaoFactory.getUserDao().deleteAll();
                rlNotEmpty.setVisibility(View.GONE);
                llEmpty.setVisibility(View.VISIBLE);
                break;
            case R.id.fragment_car_gohome_btn:
                ((RadioButton)FragmentTabUtils.rgs.getChildAt(0)).setChecked(true);
                break;
            case R.id.fragment_car_notempty_pay_btn:
                Toast.makeText(getActivity(),"确定支付吗？",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
